﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Incendios0_1.Camera.wpfdatamanager;
using AsyncCamera.Camera.Pelco;

namespace Incendios0_1.Camera.localization
{
    /// <summary>
    /// Interaction logic for localizacion.xaml
    /// </summary>
    public partial class localizacion : Window
    {
        public localizacion()
        {
            InitializeComponent();
        }

        public void muestra_mapa(double camlat, double camlng, double lat, double lng)
        {//Mediante el método muestra mapa, mostramos el mapa, ubicando la posicion de las coordenadas calculadas
            //Obtenemos la información de la latitud y longitud calculadas
            string latitud = Convert.ToString(lat);
            string longitud = Convert.ToString(lng);
            string camlatitud = Convert.ToString(camlat);
            string camlongitud = Convert.ToString(camlng);
            string url5 = "http://maps.google.com.mx/maps?q="+latitud+","+longitud+"&num=1&t=h&sll="+camlatitud+","+ camlongitud+"&sspn=0.002672,0.005252&ie=UTF8&ll="+"19.098458"+","+"-98.263092"+"&spn=0.201792,0.513611&z=11";
            webBrowser1.Navigate(url5);
            webBrowser1.UpdateLayout();
        }

        public void addActiveCamera(AbstractCamera camera)
        {
            if (camera is PelcoCamera)
            {
                listBox1.Items.Add(camera);
                if (listBox1.SelectedIndex < 0)
                    listBox1.SelectedIndex = 0;
                listBox1.UpdateLayout();
            }
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PelcoCamera selected = listBox1.SelectedItem as PelcoCamera;
            if (selected != null)
            {
                double camlat = (selected.Latitud == null) ? 0 : (double)selected.Latitud;
                double camlng = (selected.Longitud == null) ? 0 : (double)selected.Longitud;
                double lat = (selected.detectedLatitud == null) ? camlat : (double)selected.detectedLatitud;
                double lng = (selected.detectedLongitud == null) ? camlng : (double)selected.detectedLongitud;
                if (selected.detectedLatitud == null)
                    label1.Content = "Imposible calcular localización del incendio";
                else
                    label1.Content = lat + " " + lng;
                muestra_mapa(camlat, camlng, lat, lng);
            }
        }
    }
}
